Completed
Push — master ( d206cf...46a016 )
by Rafael S.
06:08
created

NumberBuffer.constructor   A

Complexity

Conditions 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
dl 0
loc 15
rs 9.9
c 0
b 0
f 0
1
/*
2
 * Copyright (c) 2017-2018 Rafael da Silva Rocha.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining
5
 * a copy of this software and associated documentation files (the
6
 * "Software"), to deal in the Software without restriction, including
7
 * without limitation the rights to use, copy, modify, merge, publish,
8
 * distribute, sublicense, and/or sell copies of the Software, and to
9
 * permit persons to whom the Software is furnished to do so, subject to
10
 * the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
/**
26
 * @fileoverview Sserialize and deserialize integers and floats.
27
 * @see https://github.com/rochars/byte-data
28
 */
29
30
import {validateFloatType, validateIntType} from './validation.js';
31
import {IEEE754Buffer} from 'ieee754-buffer';
32
import TwosComplementBuffer from 'twos-complement-buffer';
33
import UintBuffer from 'uint-buffer';
34
35
/**
36
 * A class to pack and unpack integers and floating-point numbers.
37
 * Signed integers are two's complement.
38
 * Floating-point numbers are IEEE 754 standard.
39
 */
40
export default class NumberBuffer {
41
  
42
  /**
43
   * Read one number from a byte buffer.
44
   * @param {number} bits The number of bits of the number.
45
   * @param {boolean} fp Tue for floating-point numbers.
46
   * @param {boolean} signed True for signed numbers.
47
   * @throws {Error} If the type definition is not valid.
48
   */
49
  constructor(bits, fp, signed) {
50
    /** @type {TwosComplementBuffer|UintBuffer|IEEE754Buffer} */
51
    let parser;
52
    if (fp) {
53
      validateFloatType(bits);
54
      parser = this.getFPParser_(bits);
55
    } else {
56
      validateIntType(bits);
57
      parser = signed ? new TwosComplementBuffer(bits) : new UintBuffer(bits);
58
    }
59
    /** @type {TwosComplementBuffer|UintBuffer|IEEE754Buffer} */
60
    this.parser = parser;
61
    /** @type {number} */
62
    this.offset = Math.ceil(bits / 8);
63
  }
64
  
65
  /**
66
   * Read one number from a byte buffer.
67
   * @param {!Uint8Array|!Array<number>} buffer An array of bytes.
68
   * @param {number=} index The index to read.
69
   * @return {number} The number.
70
   * @throws {Error} On overflow.
71
   */
72
  unpack(buffer, index=0) {
73
    return this.parser.unpack(buffer, index);
74
  }
75
76
  /**
77
   * Write one number to a byte buffer.
78
   * @param {!Uint8Array|!Array<number>} buffer An array of bytes.
79
   * @param {number} num The number.
80
   * @param {number=} index The index being written in the byte buffer.
81
   * @return {number} The next index to write on the byte buffer.
82
   * @throws {Error} If num is NaN.
83
   * @throws {Error} On overflow.
84
   */
85
  pack(buffer, num, index=0) {
86
    return this.parser.pack(buffer, num, index);
87
  }
88
89
  /**
90
   * Return a instance of IEEE754Buffer.
91
   * @param {number} bits The number of bits.
92
   * @return {IEEE754Buffer}
93
   * @private
94
   */
95
  getFPParser_(bits) {
96
    if (bits === 16) {
97
      return new IEEE754Buffer(5, 11);
98
    } else if(bits === 32) {
99
      return new IEEE754Buffer(8, 23);
100
    } else {
101
      return new IEEE754Buffer(11, 52);
102
    }
103
  }
104
}
105